﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BusinessLogic;
using BusinessLogic.Statuses;
using BusinessLogic.Data;

namespace WebSite.WebServices
{
    /// <summary>
    /// Summary description for UIBeckendService
    /// </summary>
    [WebService(Namespace = "http://sagi-test.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class UIBeckendService : System.Web.Services.WebService
    {
        private User CurrentUser
        {
            get
            {
                User user = (Session["UserInfo"] as User);
                if (user != null)
                {
                    return user;
                }

                if (HttpContext.Current.User != null &&
                    HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    user = new BLHelper().GetUser(HttpContext.Current.User.Identity.Name);
                    if (user != null)
                    {
                        Session["UserInfo"] = user;
                    }

                }

                return user;
            }
        }


        [WebMethod(EnableSession=true)]
        public WebinarsResultStatus GetWebinars(int pageNum, int pageSize)
        {
            User user = CurrentUser;

            WebinarsResultStatus status =  new BLHelper().GetExistingWebinars(user == null ? -1 : user.ID, pageNum, pageSize);

            status.CurrentUser = user;
            return status;
        }

        [WebMethod(EnableSession = true)]
        public WebinarsResultStatus GetFutureWebinars(int pageNum, int pageSize)
        {
            WebinarsResultStatus status = new BLHelper().GetFutureWebinars(pageNum, pageSize);

            status.CurrentUser = CurrentUser;
            return status;
        }

        [WebMethod(EnableSession = true)]
        public QuestionsResultStatus GetQuestions(int webinarId)
        {
            User user = CurrentUser;
            if (user == null)
            {
                return new QuestionsResultStatus
                {
                    NativeError = new Exception("need to log in."),
                    Status = ResultStatus.StatusType.Error
                };
            }

            return new BLHelper().GetQuestions(user.ID, webinarId);
        }

        [WebMethod(EnableSession = true)]
        public CommentsResultStatus GetComments(int questionId)
        {
            User user = CurrentUser;
            if (user == null)
            {
                return new CommentsResultStatus
                {
                    NativeError = new Exception("need to log in."),
                    Status = ResultStatus.StatusType.Error
                };
            }

            return new BLHelper().GetComments(questionId);
        }


        [WebMethod(EnableSession = true)]
        public QuestionResultStatus AddQuestion(int webinarId, string content)
        {
            User user = CurrentUser;
            if (user == null)
            {
                return new QuestionResultStatus
                {
                    NativeError = new Exception ("need to log in."),
                    Status = ResultStatus.StatusType.Error
                };
            }


            return new BLHelper().AddQuestion(user.ID, webinarId, content);
        }

        [WebMethod(EnableSession = true)]
        public ResultStatus AddComment(int questionId, string content)
        {
            User user = CurrentUser;
            if (user == null)
            {
                return new ResultStatus
                {
                    NativeError = new Exception("need to log in."),
                    Status = ResultStatus.StatusType.Error
                };
            }


            return new BLHelper().AddComment(user.ID, questionId, new Comment
            {
                UserID = user.ID,
                UserName = user.Name,
                Content = content
            });
        }

        [WebMethod(EnableSession = true)]
        public ResultStatus Vote(int questionId, bool thumbsUp)
        {
            User user = CurrentUser;
            if (user == null)
            {
                return new ResultStatus
                {
                    NativeError = new Exception("need to log in."),
                    Status = ResultStatus.StatusType.Error
                };
            }
            return new BLHelper().Vote(user.ID, questionId, thumbsUp);
        }

        [WebMethod(EnableSession = true)]
        public ResultStatus MarkAsAnswerd(int questionId)
        {
            User user = CurrentUser;
            if (user == null)
            {
                return new ResultStatus
                {
                    NativeError = new Exception("need to log in."),
                    Status = ResultStatus.StatusType.Error
                };
            }
            return new BLHelper().MarkAsAnswerd(user.ID, questionId);
        }

        [WebMethod(EnableSession = true)]
        public WebinarResultStatus ScheduleWebinar(string webinarName, DateTime scheduled, string description)
        {
            User user = CurrentUser;
            if (user == null || !user.IsAdmin)
            {
                return new WebinarResultStatus
                {
                    NativeError = new Exception("need to log in as admin."),
                    Status = ResultStatus.StatusType.Error
                };
            }
            return new BLHelper().AddWebinar(user.ID, webinarName, scheduled, description);
        }
    }
}
